package controllers

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"tizhi_front/nspel"
	"tizhi_front/request"
	"tizhi_front/response"
	"tizhi_front/utils"
)

type MsmCodeController struct {
	Ctx iris.Context
}

func (c *MsmCodeController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/", "GetCode")
}

// @Title get msm code
// @Description 获取手机验证码，根据type字段来区分不同类型，0:绑定 1:重置
// @Tags msm_code
// @Param	body body request.MsmRequest true " "
// @Success 0	{object} response.JsonResponse
// @Failure 1 {string} string "已经获取，本次获取相同的结果"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /msm_code [post]
func (c *MsmCodeController) GetCode() response.JsonResponse {
	req := request.MsmRequest{}
	err := c.Ctx.ReadJSON(&req)
	if err != nil {
		return response.InvalidParameters
	}
	if !req.Validate() {
		return response.InvalidParameters
	}
	phone := utils.Phone{Phone: req.Phone}
	ok, err := phone.ValidIsActive(req.Type)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	if ok {
		return response.InvalidParameters
	}
	err = phone.SendSms(req.Type)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    "",
	}
}
